package com.foodorder.controller;

import com.foodorder.service.CartService;
import com.foodorder.utils.ResponseResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 购物车控制器 - 处理购物车云同步
 */
@Slf4j
@RestController
@RequestMapping("/cart")
@RequiredArgsConstructor
public class CartController {

    private final CartService cartService;

    /**
     * 同步购物车
     * 将本地购物车数据同步到云端
     */
    @PostMapping("/sync")
    public ResponseEntity<?> syncCart(@RequestBody Map<String, Object> syncData) {
        try {
            Long userId = Long.valueOf(syncData.get("userId").toString());
            String deviceId = (String) syncData.get("deviceId");
            List<Map<String, Object>> cartItems = (List<Map<String, Object>>) syncData.get("cartItems");
            
            log.info("收到购物车同步请求：用户ID={}, 设备ID={}, 商品数量={}", userId, deviceId, cartItems.size());
            
            cartService.saveCartItems(userId, deviceId, cartItems);
            
            return ResponseEntity.ok(ResponseResult.success("购物车同步成功"));
        } catch (Exception e) {
            log.error("购物车同步失败：", e);
            return ResponseEntity.badRequest().body(ResponseResult.error("购物车同步失败：" + e.getMessage()));
        }
    }

    /**
     * 获取云端购物车
     */
    @GetMapping("/fetch")
    public ResponseEntity<?> fetchCart(@RequestParam Long userId, 
                                     @RequestParam(required = false) String deviceId) {
        try {
            Map<String, Object> cartData = cartService.getCartItems(userId, deviceId);
            
            log.info("获取购物车数据：用户ID={}, 设备ID={}", userId, deviceId);
            
            return ResponseEntity.ok(ResponseResult.success(cartData));
        } catch (Exception e) {
            log.error("获取购物车失败：", e);
            return ResponseEntity.badRequest().body(ResponseResult.error("获取购物车失败：" + e.getMessage()));
        }
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("/clear")
    public ResponseEntity<?> clearCart(@RequestParam Long userId) {
        try {
            cartService.clearCart(userId);
            
            log.info("清空购物车：用户ID={}", userId);
            
            return ResponseEntity.ok(ResponseResult.success("购物车已清空"));
        } catch (Exception e) {
            log.error("清空购物车失败：", e);
            return ResponseEntity.badRequest().body(ResponseResult.error("清空购物车失败：" + e.getMessage()));
        }
    }

    /**
     * 获取购物车商品数量
     */
    @GetMapping("/count")
    public ResponseEntity<?> getCartCount(@RequestParam Long userId) {
        try {
            int count = cartService.getCartItemCount(userId);
            
            return ResponseEntity.ok(ResponseResult.success(count));
        } catch (Exception e) {
            log.error("获取购物车商品数量失败：", e);
            return ResponseEntity.badRequest().body(ResponseResult.error("获取购物车商品数量失败：" + e.getMessage()));
        }
    }
} 